package com.pfzy.lib.sdk.utils;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * 活动状态监视器
 */
public class ActivityStateMonitor implements Application.ActivityLifecycleCallbacks {
    private static WeakReference<Activity> currentActivity;
    private static ActivityStateMonitor mAppStateMonitor;
    private static int mActivityCount = 0;
    private static List<ActivityStateChangeCallback> sActivityStateChangeCallbacks = new ArrayList();
    private static boolean isAppOnForeground = true;

    public interface ActivityStateChangeCallback {
        /**
         * 更改为后台
         */
        void changeToBackground();

        /**
         * 更改为前台
         */
        void changeToForeground();
    }

    public static void init() {
        if (mAppStateMonitor == null) {
            mAppStateMonitor = new ActivityStateMonitor();
            ((Application) Common.getContext().getApplicationContext()).registerActivityLifecycleCallbacks(mAppStateMonitor);
        }
    }

    public static Activity getCurrentActivity() {
        if (currentActivity == null) {
            return null;
        }
        Activity activity = currentActivity.get();
        return activity;
    }

    private static void setCurrentActivity(Activity currentActivity2) {
        if (currentActivity2 == null) {
            currentActivity = null;
        } else {
            currentActivity = new WeakReference<>(currentActivity2);
        }
    }

    /**
     * 应用程序位于前台的活动计数 是否大于 0
     * @return
     */
    public static boolean isAppOnForeground() {
        return mActivityCount > 0;
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        setCurrentActivity(activity);
    }

    @Override
    public void onActivityStarted(Activity activity) {
        setCurrentActivity(activity);
        mActivityCount++;
        checkAppForegroundState();
    }

    @Override
    public void onActivityResumed(Activity activity) {
        setCurrentActivity(activity);
    }

    @Override
    public void onActivityPaused(Activity activity) {
    }

    @Override
    public void onActivityStopped(Activity activity) {
        mActivityCount--;
        checkAppForegroundState();
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        Activity activityCurr = getCurrentActivity();
        if (activityCurr != null && activityCurr == activity) {
            setCurrentActivity(null);
        }
    }

    public static void addActivityStateChangeCallback(ActivityStateChangeCallback callback) {
        if (!sActivityStateChangeCallbacks.contains(callback)) {
            sActivityStateChangeCallbacks.add(callback);
        }
    }

    public static void removeActivityStateChangeCallback(ActivityStateChangeCallback callback) {
        sActivityStateChangeCallbacks.add(callback);
    }

    private static void checkAppForegroundState() {
        boolean isForeground = isAppOnForeground();
        if (isForeground) {
            try {
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (!isAppOnForeground) {
                for (ActivityStateChangeCallback callback : sActivityStateChangeCallbacks) {
                    callback.changeToForeground();// 更改为前台
                }
                isAppOnForeground = isForeground;
            }
        }
        if (!isForeground && isAppOnForeground) {
            for (ActivityStateChangeCallback callback2 : sActivityStateChangeCallbacks) {
                callback2.changeToBackground();// 更改为后台
            }
        }
        isAppOnForeground = isForeground;
    }
}
